<?php
class ControllerLocalisationReturnReason extends AdminController {

  	public function index() {

		M('localisation/return_reason');

    	$this->getList();
  	}

  	public function insert() {

		M('localisation/return_reason');

		if ($this->request->isPost() && $this->validateForm()) {
      		$this->model_localisation_return_reason->addReturnReason($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('localisation/return_reason'));
		}

    	$this->getForm();
  	}

  	public function update() {

		M('localisation/return_reason');

    	if ($this->request->isPost() && $this->validateForm()) {
	  		$this->model_localisation_return_reason->editReturnReason($this->request->get['return_reason_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('localisation/return_reason'));
    	}

    	$this->getForm();
  	}

  	public function delete() {

		M('localisation/return_reason');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $return_reason_id) {
				$this->model_localisation_return_reason->deleteReturnReason($return_reason_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('localisation/return_reason'));
   		}

    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$return_reason_total = $this->model_localisation_return_reason->getTotalReturnReasons();
		$this->data['return_reasons'] = $this->model_localisation_return_reason->getReturnReasons($qf);

		$this->data['pagination'] = $this->pagination($return_reason_total)->render();

		$this->display('localisation/return_reason_list.tpl');
  	}

  	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['return_reason_id'])) {
			$this->data['action'] = UA('localisation/return_reason/update', 'return_reason_id=' . $this->request->get['return_reason_id']);
		} else {
			$this->data['action'] = UA('localisation/return_reason/insert');
		}

		$this->data['languages'] = C('cache_language_2');

		if (isset($this->request->post['return_reason'])) {
			$this->data['return_reason'] = $this->request->post['return_reason'];
		} elseif (isset($this->request->get['return_reason_id'])) {
			$this->data['return_reason'] = $this->model_localisation_return_reason->getReturnReasonDescriptions($this->request->get['return_reason_id']);
		} else {
			$this->data['return_reason'] = array();
		}

		$this->display('localisation/return_reason_form.tpl');
  	}

	protected function validateForm() {

		$pass = true;

    	foreach ($this->request->post['return_reason'] as $language_id => $value) {
      		if (!range_length($value['name'], 3, 32)) {
        		$this->setMessage('error_name_' . $language_id, $this->_['error_name']);
				$pass = false;
      		}
    	}

		return $pass;
  	}

  	protected function validateDelete() {

		M('sale/return');

		foreach ($this->request->post['selected'] as $return_reason_id) {
			$return_total = $this->model_sale_return->getTotalReturnsByReturnReasonId($return_reason_id);

			if ($return_total) {
	  			$this->setMessage('error_warning', sprintf($this->_['error_return'], $return_total));
				return false;
			}
	  	}

		return true;
  	}
}
?>